<template>
  <!-- 计算公式维护编辑 -->
  <base-breadcrumb :title="$route.meta.title">
    <a-spin :spinning="loading">
      <a-card style="margin-bottom:80px">
        <a-tabs :activeKey="currentStep" @change="callback">
          <a-tab-pane key="1" tab="项目基本情况"> <projectInfo :form="form" @nextStep="changeStep('2')"></projectInfo></a-tab-pane>
          <a-tab-pane key="2" tab="目标完成情况"> <targetComplete :form="form" @previousStep="changeStep('1')" @nextStep="changeStep('3')"></targetComplete> </a-tab-pane>
          <a-tab-pane key="3" tab="项目策划执行情况"> <planningPerform :form="form" @previousStep="changeStep('2')" @nextStep="changeStep('4')"></planningPerform> </a-tab-pane>
          <a-tab-pane key="4" tab="策划检查履职情况"> <sinceTheSituation :form="form" @previousStep="changeStep('3')" @nextStep="changeStep('5')"></sinceTheSituation> </a-tab-pane>
          <a-tab-pane key="5" tab="重要节点情况"> <nodeCompletion :form="form" @previousStep="changeStep('4')" @nextStep="changeStep('6')"></nodeCompletion> </a-tab-pane>
          <a-tab-pane key="6" tab="下月重点安排"> <nextMonthArrange :form="form" @previousStep="changeStep('5')" @nextStep="changeStep('7')"></nextMonthArrange></a-tab-pane>
          <a-tab-pane key="7" tab="附件"> <attachment :form="form" @previousStep="changeStep('6')"></attachment></a-tab-pane>
        </a-tabs>
      </a-card>
      <!-- 页脚操作栏 -->
      <div class="btn_box tool-btns--fixed page-btn-right-top" v-if="editModel != 0">
        <a-button class="button_right" type="primary" :loading="loadingBtn.DRAFT" @click="handleSubmit(0)">保存草稿</a-button>
        <a-button class="button_right" type="primary" :loading="loadingBtn.SUBMITTED" @click="handleSubmit(1)">提交</a-button>
        <a-button @click="handleActiveClose">关闭</a-button>
      </div>
      <div class="btn_box tool-btns--fixed page-btn-right-top" v-else>
        <a-button @click="handleActiveClose">关闭</a-button>
      </div>
    </a-spin>
  </base-breadcrumb>
</template>

<script>
import { api } from '@/api/project/performanceAnalysis'
import BaseEditPage from '@/views/baseEditPage' // 编辑详情页基础配置
import BSelect from '@/components/BComponents/BSelect'
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 分公司
import projectInfo from './components/projectInfo'
import targetComplete from './components/targetComplete'
import planningPerform from './components/planningPerform'
import nodeCompletion from './components/nodeCompletion'
import nextMonthArrange from './components/nextMonthArrange'
import attachment from './components/attachment'
import sinceTheSituation from './components/sinceTheSituation'

export default {
  name: 'updateProjectPerformanceAnalysis',
  extends: new BaseEditPage(),
  components: {
    SelectProject,
    BSelect,
    SelectSubCompany,
    projectInfo,
    targetComplete,
    planningPerform,
    nodeCompletion,
    nextMonthArrange,
    attachment,
    sinceTheSituation
  },

  data() {
    return {
      loading: false, // 加载控件
      editModel: 1, // 操作状态:0 查看;1 编辑
      form: {}, // 表单
      id: '',
      currentStep: '1',
      loadingBtn: {
        DELETE: false,
        DRAFT: false,
        SUBMITTED: false,
        REJECTED: false
      },

      rules: { month: [{ required: true, message: '请选择' }] }, // 校验规则
      init: false // 初始化控件
    }
  },
  props: {
    isAudit: {
      // 是否作为审批详情
      type: Boolean,
      default: false
    }
  },
  watch: {
    isAudit: {
      immediate: true,
      handler(val) {
        if (val) {
          this.$nextTick(() => {
            this.editModel = 0
          })
        }
      }
    }
  },
  mounted() {
    // 不为添加时请求详情
    this.$route.meta.title = '公司工程履约管理'
    if (this.editModel != 2) {
      this.getDetailData()
    } else {
      this.$nextTick(() => {
        // 默认映射联系人为登陆人

        this.init = true
      })
    }
  },

  methods: {
    callback(step) {
      this.currentStep = step
    },
    changeStep(nextStep) {
      this.currentStep = nextStep
    },

    getDetailData() {
      this.loading = true
      api
        .getLvyueCompany({ id: this.id })
        .then((res) => {
          if(res.data){
            // 添加主键
            res.data.companyVO?.resourcesVOList?.forEach((item,index) => {
              item.key = index
            })
            this.form = { ...res.data }
            this.form.keyrateVO.projectOrgId = this.form.projectVO?.projectOrgId
            this.form.keyrateVO.branchCompanyOrgId = this.form.projectVO?.branchCompanyId
          }
          this.$nextTick(() => {
            this.init = true
          })
        })
        .finally(() => {
          this.loading = false
        })
    },

    /**
     * 提交表单
     */
    validateForm() {
      let flag1 = true,
        flag2 = true,
        flag3 = true,
        flag4 = true,
        flag5 = true,
        flag6 = true,
        flag7 = true

      //校验风险情况统计
      if (this.form.keyrateVO.riskDTOList.filter(i=> !i.isDeleted)?.length > 0) {
        let list = ['takeSteps', 'complete', 'branchCompanyDutyId', 'projectDutyId', 'endTime']
        this.form.keyrateVO.riskDTOList.filter(i=> !i.isDeleted)?.forEach((obj) => {
          Object.keys(obj).forEach((key) => {
            if (list.indexOf(key) != -1 && !obj[key]) {
              flag1 = false
            }
          })
        })
      } else {
        flag1 = false
      }
      if (this.form.keyrateVO.majorDTOList.filter(i=> !i.isDeleted)?.length > 0) {
        let list = ['designProblem', 'subProblem']
        this.form.keyrateVO.majorDTOList.filter(i=> !i.isDeleted).forEach((obj) => {
          Object.keys(obj).forEach((key) => {
            if (list.indexOf(key) != -1 && !obj[key]) {
              flag2 = false
            }
          })
        })
      } else {
        flag2 = false
      }
      if (this.form.keyrateVO.complaintDTOList.filter(i=> !i.isDeleted)?.length > 0) {
        let list = ['complaint']
        this.form.keyrateVO.complaintDTOList.filter(i=> !i.isDeleted).forEach((obj) => {
          Object.keys(obj).forEach((key) => {
            if (list.indexOf(key) != -1 && !obj[key]) {
              flag3 = false
            }
          })
        })
      } else {
        flag3 = false
      }
      if (this.form.nextmonthVO.nextplanworkDTOList.filter(i=> !i.isDeleted)?.length > 0) {
        let list = ['nextMonthWork']
        this.form.nextmonthVO.nextplanworkDTOList.filter(i=> !i.isDeleted).forEach((obj) => {
          Object.keys(obj).forEach((key) => {
            if (list.indexOf(key) != -1 && !obj[key]) {
              flag4 = false
            }
          })
        })
      } else {
        flag4 = false
      }
      if (this.form.planproblemVO.planproblemVosNode.filter(i=> !i.isDeleted)?.length > 0) {
        let list = ['problem', 'correctiveAction', 'completeTime']
        this.form.planproblemVO.planproblemVosNode.filter(i=> !i.isDeleted).forEach((obj) => {
          Object.keys(obj).forEach((key) => {
            if (list.indexOf(key) != -1 && !obj[key]) {
              flag5 = false
            }
          })
        })
      } else {
        flag5 = false
      }
      if (this.form.planproblemVO.planproblemVosSub.filter(i=> !i.isDeleted)?.length > 0) {
        let list = ['problem', 'correctiveAction', 'completeTime']
        this.form.planproblemVO.planproblemVosSub.filter(i=> !i.isDeleted).forEach((obj) => {
          Object.keys(obj).forEach((key) => {
            if (list.indexOf(key) != -1 && !obj[key]) {
              flag6 = false
            }
          })
        })
      } else {
        flag6 = false
      }
      if (this.form.planproblemVO.planproblemVosTechnology.filter(i=> !i.isDeleted)?.length > 0) {
        let list = ['problem', 'correctiveAction', 'completeTime']
        this.form.planproblemVO.planproblemVosTechnology.filter(i=> !i.isDeleted).forEach((obj) => {
          Object.keys(obj).forEach((key) => {
            if (list.indexOf(key) != -1 && !obj[key]) {
              flag7 = false
            }
          })
        })
      } else {
        flag7 = false
      }

      if (!flag1) {
        this.$message.warning('风险情况统计表格输入不完整')
      }
      if (!flag2) {
        this.$message.warning('影响履约重大事项表格输入不完整')
      }
      if (!flag3) {
        this.$message.warning('投诉表格输入不完整')
      }
      if (!flag4) {
        this.$message.warning('下月策划执行管理工作安排输入不完整')
      }
      if (!flag5) {
        this.$message.warning('节点、劳动力管控问题表格输入不完整')
      }
      if (!flag6) {
        this.$message.warning('分包、分供招采问题表格输入不完整')
      }
      if (!flag7) {
        this.$message.warning('技术、设计问题表格输入不完整')
      }

      if (flag1 && flag2 && flag3 && flag4 && flag5 && flag6 && flag7) {
        return true
      } else {
        return false
      }
    },

    /**
     * 提交表单
     */
    handleSubmit(status) {
      if (status == 1 && !this.validateForm()) {
        return
      }

      let form = this.$clone(this.form)
       
      this.loading = true
      form.auditStatus = status
      api
        .updateLvyueCom(form)
        .then((res) => {
          console.log(res)
          if (res.code == 200) {
            this.loading = false
            this.$message.success(status == 'SUBMITTED' ? '提交成功！' : '保存成功')
            this.$multiTab.close()
          } else {
            this.loading = false
          }
        })
        .catch(() => {
          this.loading = false
          this.$message.warning(err.data.message)
        })
    }
  }
}
</script>
<style lang="less" scoped>
/deep/ .form-item  {
  .ant-input:not([disabled]),
  .ant-select:not(.ant-select-disabled) .ant-select-selection,
  .ant-calendar-picker-input,
  .ant-input-number:not([disabled]),
  .ant-cascader-picker {
    border-color: #e4ac0b;

    background-color: #fff9e8;
  }
  .ant-input-disabled,
  .ant-input-number-disabled .ant-input-number-input,
  .ant-time-picker-input[disabled] {
    border-color: #d9d9d9 !important;
    background-color: #f5f5f5 !important;
  }

  .remark {
    .ant-input {
      border-color: inherit;
      background-color: initial;
    }
  }
}
</style>